package com.yilin.tms.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.NoLoginSecurity;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.core.entity.user.organize.MemberType;
import com.yilin.tms.core.entity.user.organize.Organize;
import com.yilin.tms.user.service.IMemberTypeService;
import com.yilin.tms.user.service.IOrganizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 * 会员
 * */
@RestController
@RequestMapping("/user/memberType/")
public class MemberTypeController extends BaseController {

    @Autowired
    private IMemberTypeService memberTypeService;
    @Autowired
    private IOrganizeService organizeService;

    /**
     * 获取会员类型列表
     */
    @PostMapping("getMemberTypeListByOrganizeId")
    public ReturnData<List<MemberType>> getMemberTypeListByOrganizeId(String organizeId, MemberType query) {
        if (organizeId == null) organizeId = getLoginUser().getOrganizeId();
        List<MemberType> memberTypeList = memberTypeService.getMemberTypeListByOrganizeId(organizeId, query);
        return jsonView(true, "获取列表成功", memberTypeList);
    }

    /**
     * 分配会员类型到组织
     */
    @PostMapping("saveOrganizeMemberType")
    public ReturnData<Object> saveOrganizeMemberType(String organizeId, String memberTypeId) {
        if (StringUtil.isEmpty(organizeId)) {
            return jsonView(false, "组织不能为空！");
        }
        Organize organize = organizeService.getOrganizeById(organizeId);
        if (!getLoginUser().getOrganizeId().equals(organize.getSuperiorPlatOrganizeId())){
            return jsonView(false, "您没有权限操作该组织的会员信息！");
        }
        organizeService.saveOrUpdate(organize);
        return jsonView(true, "会员分配成功");
    }

    /**
     * 保存会员类型
     */
    @PostMapping("saveMemberType")
    public ReturnData<Object> saveMemberType(MemberType memberType) {
        if (StringUtil.isEmpty(memberType.getName())) {
            return jsonView(false, "会员类型名称不能为空！");
        }
        if (getLoginUser().getOrganizeName() == null) {
            return jsonView(false, "你的组织信息未完善，请完善组织名称后操作！");
        }
        if (getLoginUser().getUserType().index > UserType.platformUser.index) {
            return jsonView(false, "您没有权限操作会员类型！");
        }
        if (memberType.getUserType() == null) {
            memberType.setUserType(UserType.values()[getLoginUser().getUserType().index + 1]);
        }
        if (getLoginUser().getUserType().index >= memberType.getUserType().index) {
            return jsonView(false, "你添加的会员类型不支持，用户级别受限！");
        }
//        //检查付款比例
//        if (memberType.getReleasePaymentRate()+memberType.getRobPaymentRate()+memberType.getDeliverPaymentRate()+
//            memberType.getArrivalPaymentRate()!=1D){
//            return jsonView(false,"付款比例合计必须为100%，请修改付款比例");
//        }
        // #### 检查会员名称设置是否重复
        MemberType localMemberType = memberTypeService.getMemberTypeByName(getLoginUser().getOrganizeId(), memberType.getName());
        if (localMemberType != null && (memberType.getId() == null || !memberType.getId().equals(localMemberType.getId()))) {
            return jsonView(false, "会员名称重复！");
        }
        // #### 检查会员设置必须设置默认
        MemberType defaultMemberType = memberTypeService.getDefaultMemberTypeByOrganizeId(getLoginUser().getOrganizeId(), memberType.getUserType());
        if (!memberType.getIsDefault()) { // ### 替换默认会员类型
            if (defaultMemberType == null || (memberType.getId() != null && memberType.getId().equals(defaultMemberType.getId()))) {
                return jsonView(false, "您暂时还没有添加其他默认的会员类型，请勾选'默认'！");
            }
        } else if (defaultMemberType != null && (memberType.getId() == null || !memberType.getId().equals(defaultMemberType.getId()))) {
            defaultMemberType.setIsDefault(false);
            memberTypeService.saveOrUpdate(defaultMemberType);
        }
        memberType.setStatus(true);
        memberType.setOrganizeId(getLoginUser().getOrganizeId());
        memberType.setOrganizeName(getLoginUser().getOrganizeName());
        memberTypeService.saveOrUpdate(memberType);
        return jsonView(true, "保存成功！");
    }

    /**
     * 删除 配置数据直接删除
     * 需检查 账户信息
     */
    @PostMapping("deleteMemberType")
    public ReturnData<Object> deleteMemberType(MemberType memberType) {
        if (memberType.getId() == null) {
            return jsonView(false, "未指定数据！");
        }
        List<Organize> organizeList = organizeService.getOrganizeListByMemberTypeId(memberType.getId());
        if (organizeList.size() > 0) {
            return jsonView(false, "会员下存在用户，请先将使用的用户替换会员！");
        }
        MemberType localMemberType = memberTypeService.getMemberTypeById(getLoginUser().getOrganizeId(), memberType.getId());
        if (localMemberType.getIsDefault()) {
            return jsonView(false, "不能删除默认会员类型，请先设定其他会员类型为默认！");
        }
        memberTypeService.removeById(memberType);
        return jsonView(true, "删除成功！");
    }

    /**
     * 根据会员ID获取会员
     * */
    @PostMapping("getMemberTypeById")
    @NotNull(value = {"organizeId","memberTypeId"})
    @SkipLoginVerify
    public ReturnData<Object> getMemberTypeById(@Explain("组织ID") String superiorOrganizeId,@Explain("会员ID") String memberTypeId) {
        MemberType memberType =  memberTypeService.getMemberTypeById(superiorOrganizeId,memberTypeId);
        return jsonView(true, "获取成功！",memberType);
    }

    /**
     * 获取我组织 会员
     */
    @PostMapping("getMyMemberTypeByOrganizeId")
    @NotNull(value = {"organizeId","memberTypeId"})
    @NoLoginSecurity
    public ReturnData<MemberType> getMyMemberTypeByOrganizeId(String organizeId) {
        if (organizeId == null) organizeId = getLoginUser().getOrganizeId();
        Organize organize = organizeService.getOrganizeById(organizeId);
        if (organize==null)  return jsonView(true, "获取我的会员类型成功", null);
//        MemberType memberType = memberTypeService.getMemberTypeById(organize.getSuperiorPlatOrganizeId(),organize.getMemberTypeId());
        return jsonView(true, "获取我的会员类型成功", null);
    }

    /**
     * 获取默认会员
     */
    @PostMapping("getDefaultMemberType")
    @NotNull(value = {"organizeId","userType"})
    public ReturnData<MemberType> getDefaultMemberType(@Explain("组织ID") String superiorOrganizeId, @Explain("用户类型") UserType userType){
        QueryWrapper<MemberType> queryWrapper = new QueryWrapper<MemberType>();
        queryWrapper.eq("organize_id",superiorOrganizeId);
        queryWrapper.eq("user_type", userType.index);
        queryWrapper.eq("is_default", 1);
        MemberType memberType = memberTypeService.getOne(queryWrapper);
        return jsonView(true,"获取成功",memberType);
    }


}
